package com.code.commons.util;

/**
 * @author: hongchun
 * @date: 2020-01-21 19:56
 * <p>
 * 签名算法
 * <p>
 */

import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;
import java.security.MessageDigest;

/**
 * 签名算法
 */
@Slf4j
public class SignatureUtil {

    public static String signature(String key, String data, String type, String charset) {
        String sign = "";
        try {
            MessageDigest digit = MessageDigest.getInstance(type);
            digit.update(join(new byte[][]{key.getBytes(),
                    data.getBytes(Charset.forName(charset))}));
            byte[] sign2 = digit.digest();
            sign = encode(sign2);
        } catch (Exception e) {
            log.error("signature error:", e);
        }
        return sign;
    }

    public static String encode(byte[] src) {
        StringBuffer hs = new StringBuffer();
        for (int i = 0; i < src.length; i++) {
            String stmp = Integer.toHexString(src[i] & 0xFF).toUpperCase();
            if (stmp.length() == 1)
                hs.append("0").append(stmp);
            else {
                hs.append(stmp);
            }
        }
        return hs.toString();
    }

    public static byte[] join(byte[][] byteArrs) {
        if (byteArrs == null) {
            return null;
        }
        if (byteArrs.length == 0) {
            return new byte[0];
        }
        int length = 0;
        for (byte[] byteArr : byteArrs) {
            if ((byteArr != null) && (byteArr.length != 0))
                length += byteArr.length;
        }
        byte[] result = new byte[length];
        int index = 0;
        for (byte[] byteArr : byteArrs)


            if ((byteArr != null) && (byteArr.length != 0)) {
                System.arraycopy(byteArr, 0, result, index, byteArr.length);
                index += byteArr.length;
            }
        return result;
    }

//    public static void main(String[] args) {
//        Map data = new LinkedHashMap();
//        data.put("businessNo", "B100000");
//        data.put("tag", "recommend");
//        data.put("requestId", "12345678");
//        data.put("id", "");
//        //获取sign
//        String data1 = JsonUtil.object2Json(data);
//        String sign = signature("bkb88888888", data1, "SHA-512", "UTF8");
////        String sign = signature("07360FD5946F9F7620942E18EA9EFF48", JsonUtil.object2Json(data), "SHA-512", "UTF8");
//        System.out.println("sign:"+sign);
//        Map remoteData = new HashMap();
////        remoteData.put("channel", "swft");
//        remoteData.put("channel", "xt");
//        remoteData.put("sign", sign);
//        remoteData.put("data", data);
//        System.out.println("remoteData:"+JsonUtil.object2Json(remoteData));
//        RestTemplate restTemplate = new RestTemplate();
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_JSON);
//        HttpEntity<Map> entity = new HttpEntity<>(remoteData, headers);
//        HttpEntity<String> response = restTemplate
//                .exchange("http://api-qa.beekuaibao.com/thirdparty/getOpenData/V2",
//                        HttpMethod.POST, entity, String.class);
//        System.out.println("获取数据:" + response.getBody());
//    }
}
